package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.ImageViewInfo
import com.hinnotek.warehouse.bean.MachineAccountBean
import com.hinnotek.warehouse.bean.MachineListBean
import com.hinnotek.warehouse.databinding.ActivityMachineAccountListBinding
import com.hinnotek.warehouse.databinding.ItemMachineListNoLayoutBinding
import com.hinnotek.warehouse.viewmodel.MachineAccountViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_DETAIL
import com.lee.common.utils.ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_LIST
import com.lee.common.utils.Skipping
import dagger.hilt.android.AndroidEntryPoint

/**
 * 台账列表
 */
@Route(path = ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_LIST)
@AndroidEntryPoint
class MachineAccountListActivity : BaseActivity<ActivityMachineAccountListBinding>() {


    private lateinit var adapter: SimpleBindingAdapter<MachineListBean, ItemMachineListNoLayoutBinding>

    private lateinit var layoutManager: LinearLayoutManager
    private val qrList by lazy { arrayListOf<ImageViewInfo>() }

    private val viewModel by viewModels<MachineAccountViewModel>()
    var machineAccountBean: MachineAccountBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_machine_account_list)
        setTitle("台账汇总")

        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()

        machineAccountBean = intent.getSerializableExtra(MACHINE_CONTENT_LIST) as MachineAccountBean
        bindingView.machineData = machineAccountBean

        viewModel.applyId = intent.getIntExtra("applyId",0)
    }

    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_machine_list_no_layout,
            ArrayList(),
            BR.machineList
        )
        layoutManager = LinearLayoutManager(this)
        bindingView.recycler.layoutManager = layoutManager
        bindingView.recycler.adapter = adapter

        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }

        adapter.addListener { binding, itemData ->
             binding.root.setOnClickListener {
                 Skipping.start(ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_DETAIL,"applyDeviceId",itemData.applyDeviceId,MACHINE_CONTENT_LIST,machineAccountBean,MACHINE_DETAIL_LIST,itemData,this@MachineAccountListActivity)
             }
        }

    }
    
    override fun setData() {
        viewModel.machineAccountListPage.observe(this, Observer {
            adapter.addAll(it, true)
        })
    }
}